﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class KC_Add : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string op = Request.QueryString["op"];
                this.hdId.Value = op;
                string title = "新增{0}库存";
                this.lblTitle.Text = string.Format(title, op.Equals("01") ? "物料" : "产品");
                initValue();
            }
        }

        private void initValue()
        {
            if (this.hdId.Value.Equals("01"))
            {
                InitWL();
            }
            else
            {
                InitCP(); 
            }

            // 仓位
            ZL_CW_Dao daoCW = new ZL_CW_Dao();
            List<ZL_CW> lsCW = daoCW.QueryAll();
            this.ddlCWID.DataSource = lsCW;
            this.ddlCWID.DataValueField = "ID";
            this.ddlCWID.DataTextField = "BH";
            this.ddlCWID.DataBind();
            this.ddlCWID.Items.Insert(0, new ListItem("请选择", "0"));

            // 仓库
            ZL_CK_Dao daoCK = new ZL_CK_Dao();
            List<ZL_CK> lsCK = daoCK.QueryAll();
            this.ddlCKID.DataSource = lsCK;
            this.ddlCKID.DataValueField = "ID";
            this.ddlCKID.DataTextField = "MC";
            this.ddlCKID.DataBind();
            this.ddlCKID.Items.Insert(0, new ListItem("请选择", "0"));

            // 单位
            CS_JBDW_Dao daoDW = new CS_JBDW_Dao();
            List<CS_JBDW> lsDw = daoDW.Query4WL();
            this.ddlDW.DataSource = lsDw;
            this.ddlDW.DataValueField = "BH";
            this.ddlDW.DataTextField = "MC";
            this.ddlDW.DataBind();
            this.ddlDW.Items.Insert(0, new ListItem("请选择", "0"));
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
        }

        private void InitWL()
        {
            this.ddlWPID.Items.Clear();
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            List<ZL_WL> lsZP = daoWL.QueryAll();
            this.ddlWPID.DataSource = lsZP;
            this.ddlWPID.DataValueField = "ID";
            this.ddlWPID.DataTextField = "MC";
            this.ddlWPID.DataBind();
            this.ddlWPID.Items.Insert(0, new ListItem("请选择", "0"));
        }


        private void InitCP()
        {
            this.ddlWPID.Items.Clear();
            ZL_CP_Dao daoCP = new ZL_CP_Dao();
            List<ZL_CP> lsZP = daoCP.QueryAll();
            this.ddlWPID.DataSource = lsZP;
            this.ddlWPID.DataValueField = "ID";
            this.ddlWPID.DataTextField = "BH";
            this.ddlWPID.DataBind();
            this.ddlWPID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (this.ddlWPID.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择物品";
                return;
            }

            if (this.ddlCKID.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择仓库";
                return;
            }

            if (this.ddlCWID.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择仓位";
                return;
            }


            KC entity = new KC()
            {
                ID = Guid.NewGuid(),
                WPID = Guid.Parse(this.ddlWPID.SelectedValue),
                WPLX = this.hdId.Value,
                CKID = Guid.Parse(this.ddlCKID.SelectedValue),
                CWID = Guid.Parse(this.ddlCWID.SelectedValue),
                SL = int.Parse(this.tbSL.Text.Trim()),
                ZXDJ = this.tbZXDJ.Text.Trim().Equals("") ? 0.0 : float.Parse(this.tbZXDJ.Text.Trim()),
                DW = this.ddlDW.SelectedValue,
                PJDJ = int.Parse ( this.tbZXDJ.Text .Trim ())

            };

            string resultMessage = "";
            try
            {
                KC_Dao dao = new KC_Dao();
                int result = dao.Add(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
            initValue();
            this.tbSL.Text = "";
            this.tbZXDJ.Text = "";
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            if (this.hdId.Value == "01")
                Response.Redirect("~/Page/WLKC_Query.aspx");
            else
                Response.Redirect("~/Page/KC_Query.aspx?op=" + this.hdId.Value);
        }
    }
}